package com.polaris.live.im.utils

import com.lxj.xpopup.XPopup
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.common.util.ViewUtils
import com.polaris.live.dialog.common.CommonSureDetailDialog
import com.polaris.live.im.sfs.LIVE_NOTICE_MESSAGE
import com.polaris.live.im.sfs.LIVE_TOAST_MESSAGE
import com.polaris.live.im.sfs.bean.live.SendLiveMessage
import com.polaris.live.im.sfs.bean.live.payload.NoticePayloadBean

/**
 * 公共消息处理   就是不需要到某个页面单独处理的
 *
 * @author Created by 半仙 on 2023/11/21/021 14:52
 */
object PublicMessageUtils {

    fun onPublicMessage(liveMessage: SendLiveMessage?) {
        when (liveMessage?.type) {
            //这是Toast消息 往下继续分发处理没有什么意义 所以直接在这里就处理了
            LIVE_TOAST_MESSAGE -> {
                ToastGravity.showGravity(liveMessage.content)
            }
            //这是通知消息  同理继续往下分发也没有什么意义  只是展示即可
            LIVE_NOTICE_MESSAGE -> {
                val context = ViewUtils.findTopAliveActivity() ?: return

                val payload = liveMessage.payload as NoticePayloadBean
                val title = payload.title ?: return
                XPopup.Builder(context)
                    .isDestroyOnDismiss(true)
                    .asCustom(
                        CommonSureDetailDialog(
                            context,
                            title,
                            liveMessage.content,
                            payload.button
                        )
                    )
                    .show()
            }
        }
    }
}